package com.baseproject.activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.Nullable;
import android.widget.ImageView;

import com.baseproject.BaseProjectAppCofig;
import com.baseproject.R;
import com.baseproject.mvp.MvpActivity;

/**
 * 启动页
 * Created by hp on 2017/5/23.
 */

public abstract class BaseSplashActivity extends MvpActivity implements Runnable {

    ImageView splashImg;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_splash;
    }

    @Override
    public void setWindowFeature() {
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    @Override
    protected void _onCreate(@Nullable Bundle savedInstanceState) {
        disableNightMode = true;
        splashImg = (ImageView) findViewById(R.id.splash_logo);
        splashImg.setBackgroundResource(getBackgroundDrawableId());
        ObjectAnimator objectAnimator = doAnimation(splashImg);
        if (objectAnimator != null) {
            objectAnimator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    startMainActivity(500);
                }
            });
        } else {
            startMainActivity(800);
        }
    }

    protected abstract int getBackgroundDrawableId();

    private void startMainActivity(int delayMillis) {
        new Handler(Looper.getMainLooper()).postDelayed(this, delayMillis);
    }

    protected abstract ObjectAnimator doAnimation(ImageView imageView);

    @Override
    public void run() {
        startActivity(BaseProjectAppCofig.getInstance().getMainActivityClass());
        finish();
    }

}
